package com.lksy.web.controller.student;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.inject.Inject;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.lksy.core.annotation.FormModel;
import com.lksy.core.config.MapUtils;
import com.lksy.core.form.MapWapper;
import com.lksy.core.print.Printer;
import com.lksy.web.controller.BaseController;
import com.lksy.web.model.Student;
import com.lksy.web.service.student.StudentService;

/**
 * 
 * @author jzx
 * @date 2016-08-01 下午14:43:08
 * 
 * 学生管理控制器
 */
@Controller
@RequestMapping("student")
public class StudentController extends BaseController {

	
	@Inject
	private StudentService studentService;
	
	/**
	 * 学生信息列表
	 */
	@RequestMapping(value = "/list")
	public String studentList(Model model) {
		List<Student> studentList = studentService.list();
		Collections.sort(studentList,new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
			    return o2.getStid().compareTo(o1.getStid());
			}
		});
		
		model.addAttribute("data", studentList);
		return "/student/displaystudent";
	}
	
	/**
	 * 跳转到学生列表页面
	 */
	@RequestMapping(value = "/toStudentlist")
	public String toStudentlist(Model model) {
		model.addAttribute("mainPage", "/student/list");
		return "/system/main";
	}
	
	/**
	 * 跳转到添加学生信息页面
	 */
	@RequestMapping(value = "/toAddStudent")
	public String toAddStudent(Model model) {
		model.addAttribute("mainPage", "/student/addStudent");
		return "/system/main";
	}
	
	/**
	 * 跳转到添加学生信息页面
	 */
	@RequestMapping(value = "/addStudent")
	public String addStudent(Model model) {
		return "/student/studentAdd";
	}
	
	/**
	 * 跳转到修改学生信息页面
	 */
	@RequestMapping(value = "/toUpdateStudent/{id}")
	public String toUpdateStudent(@PathVariable Integer id,Model model) {
		model.addAttribute("mainPage", "/student/updateStudent/" + id);
		return "/system/main";
	}
	
	/**
	 * 跳转到修改学生信息页面
	 */
	@RequestMapping(value = "/updateStudent/{id}")
	public String updateStudent(@PathVariable Integer id,Model model) {
		Student student = studentService.findStudentBySearchId(Integer.valueOf(id));
		model.addAttribute("student", student);
		return "/student/studentUpdate";
	}
	
	
	/**
	 * 跳转到系统默认页面
	 */
	@RequestMapping(value = "/toDefault")
	public String toDefault() {
		return "/common/default";
	}
	
	/**
	 * 添加学生信息
	 */
	@RequestMapping(value = "/addStudent", method =  RequestMethod.POST)
	public String addStudent(@FormModel("map") MapWapper<String, String> map,Model model) throws InstantiationException, IllegalAccessException, InvocationTargetException {
		Student  student = new Student();
		MapUtils.toObject(student, map.toMap());
		if(studentService.save(student)) {
			model.addAttribute("msg", "添加成功");
		} else {
			model.addAttribute("msg", "添加失败");
		}
		return "/system/main";
	}
	
	/**
	 * 修改学生信息
	 */
	@RequestMapping(value = "/editStudent", method = RequestMethod.POST)
	public String editStudent(@Valid Student student,BindingResult result) {
		List<FieldError> fieldErrors = result.getFieldErrors();
		
		if (fieldErrors != null && !fieldErrors.isEmpty()) {
			String defaultMessage = fieldErrors.get(0).getDefaultMessage();
			Printer.error(defaultMessage); // 不处理异常信息，打印出来即可
		}
		
		if(studentService.update(student)) {
			Printer.info("修改学生信息成功！！！");
		} else {
			Printer.error("修改学生信息失败！！！");
		}
		return redirect("/student/toStudentlist");
	}
	
	/**
	 * 删除学生信息
	 */
	@RequestMapping(value = "/deleteStudent/{id}")
	public String deleteStudent(@PathVariable Integer id) {
		studentService.deleteStudent(id);
		return "redirect:/student/toStudentlist";
	}
	
	/**
	 * 查找摸个学生信息
	 */
	@RequestMapping(value = "findStudent") 
	public String findStudent(@RequestParam String searchId,Model model) {
		model.addAttribute("mainPage", "/student/showstudent/" + searchId);
		return "/system/main";
	}
	
	
	@RequestMapping(value = "showstudent/{id}") 
	public String showStudent(@PathVariable Integer id,Model model) {
		Student student = studentService.findStudentBySearchId(Integer.valueOf(id));
		model.addAttribute("student", student);
		return "/student/showstudent";
	}
}
